import 'package:bpb/framework/base/base_page.dart';
import 'package:bpb/framework/custom/ext_widget/widget_util.dart';
import 'package:bpb/framework/utils/sized_box_util.dart';
import 'package:bpb/pages/common.dart';
import 'package:bpb/pages/setting/setting_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:in_app_review/in_app_review.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../routers/routers.dart';

class SettingPage extends BasePage<SettingController>{

  @override
  Widget build(BuildContext context) {

    controller.awake();

    return Scaffold(
      backgroundColor: Color(0xffF5F3F1),
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        title: Text("Settings",style: GoogleFonts.poppins(
        fontWeight: FontWeight.w500,
        fontSize: 16.sp,
        color: Color(0xff333333)
      ),),

      ),
      body: Column(
        children: [
          sb.h(20.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset("images/bpr_icon_s.png"),
            ],
          ),
          sb.h(20.h),
          Obx((){
            return Text("Version:${controller.versionName}");
          }),
          sb.h(20.h),

          createDataSourceAndDisclaimer(),

          Offstage(
            offstage: true,
            child: Container(
              margin: EdgeInsets.only(left: 15.w,right: 15.w),
              padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 10.h,bottom: 10.h),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15.r),
                color: Colors.white,
                  boxShadow:[
                    BoxShadow(
                      color:Colors.black.withAlpha(100),
                      offset: Offset(3, 3),
                      blurRadius: 5
                    )
                  ]
              ),
              child: Container(
                height: 48.h,
                child: Row(
                  children: [
                    Image.asset("images/icon_books.png",width: 22.w,),
                    sb.w(12.w),
                    Text("Blood Pressure Category",style: GoogleFonts.poppins(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w500,
                      color: Color(0xff333333)
                    ),),
                    Spacer(),
                    Icon(Icons.arrow_forward_ios_sharp,size: 20.w,)
                  ],
                ),
              ),
            ).prop(onTap: (){
              Get.toNamed(AppPage.ROUTER_CATEGORY);
            }),
          ),

          sb.h(20.h),
          Container(
            margin: EdgeInsets.only(left: 15.w,right: 15.w),
            padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 10.h,bottom: 10.h),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15.r),
                color: Colors.white,
                boxShadow:[
                  BoxShadow(
                      color:Colors.black.withAlpha(100),
                      offset: Offset(3, 3),
                      blurRadius: 5
                  )
                ]
            ),
            child: Column(
              children: [

                Container(
                  height: 48.h,
                  child: Row(
                    children: [
                      Image.asset("images/icon_rate_star.png",width: 22.w,),
                      sb.w(12.w),
                      Text("Rate App",style: GoogleFonts.poppins(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w500,
                          color: Color(0xff333333)
                      ),),
                      Spacer(),
                      Icon(Icons.arrow_forward_ios_sharp,size: 20.w)
                    ],
                  ).prop(onTap: (){
                    openStoreReviewPage();
                  }),
                ),

                Divider(),

                Container(
                  height: 48.h,
                  child: Row(
                    children: [
                      Image.asset("images/icon_feed_bk.png",width: 22.w,),
                      sb.w(12.w),
                      Text("Send Feedback",style: GoogleFonts.poppins(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w500,
                          color: Color(0xff333333)
                      ),),
                      Spacer(),
                      Icon(Icons.arrow_forward_ios_sharp,size: 20.w)
                    ],
                  ).prop(onTap: (){

                    sendEmail();
                  }),
                ),
              ],
            ),
          )


        ],
      ),
    );
  }



  Future<void> sendEmail() async {
    final Uri emailUri = Uri(
      scheme: 'mailto',
      path: '2316015179@qq.com',
      query: _encodeQueryParameters(<String, String>{
        'subject': 'BPR App Feedback',
        'body': 'Hi,\n\nI would like to give some feedback...',
      }),
    );

    if (await canLaunchUrl(emailUri)) {
      await launchUrl(emailUri);
    } else {
      throw 'Could not launch email client';
    }
  }

// 编码 query 参数，避免空格等字符问题
  String? _encodeQueryParameters(Map<String, String> params) {
    return params.entries
        .map((e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
        .join('&');
  }



  Future<void> openStoreReviewPage() async {
    final InAppReview inAppReview = InAppReview.instance;

    if (await inAppReview.isAvailable()) {
      // 优先尝试弹出原生评分窗口（iOS 有可能不显示）
      await inAppReview.requestReview();
    } else {
      // 不可用时跳转到商店评分页面（iOS App Store 或 Android Play 商店）
      await inAppReview.openStoreListing(
        appStoreId: '6747628714', // iOS 的 App Store ID（必须设置）
        // androidAppPackageName: 'com.example.bpr' 默认就是当前包名
      );
    }
  }




}